<?php
if (!defined('IN_MEDIA')) die("Hacking attempt");

if ($_POST['reloadPlaylist'] && $isLoggedIn) {
	$add_id = (int)$add_id;

	$remove_id = (int)$remove_id;
	$playlist_id = m_get_data('USER',$_SESSION['user_id'],'user_playlist_id');
	if ($add_id || $remove_id) {
		$q = $mysql->query("SELECT playlist_contents FROM ".$tb_prefix."playlist WHERE playlist_id = '".$playlist_id."'");
		if (!$mysql->num_rows($q)) {
			if ($add_id) {
				$cached['playlist'] = $add_id;
				$mysql->query("INSERT INTO ".$tb_prefix."playlist (playlist_id,playlist_contents,playlist_time) VALUES ('".$playlist_id."','".$add_id."','".NOW."')");
			}
		}
		else {
			$r = $mysql->fetch_array($q);
			$playlist = $r['playlist_contents'];
			if ($remove_id) {
				if ($playlist === $remove_id) {
					$mysql->query("DELETE FROM ".$tb_prefix."playlist WHERE playlist_contents = '".$remove_id."' AND playlist_id = '".$playlist_id."'");
					$cached['playlist'] = '';
				}
				else {
					$z = explode(',',$playlist);
					if (in_array($remove_id,$z)) {
						unset($z[array_search($remove_id,$z)]);
						$str = implode(',',$z);
						if (!$str) {
							$mysql->query("DELETE FROM ".$tb_prefix."playlist WHERE playlist_contents = '".$remove_id."' AND playlist_id = '".$playlist_id."'");
							$cached['playlist'] = '';
						}
						else {
							$mysql->query("UPDATE ".$tb_prefix."playlist SET playlist_contents = '".$str."' WHERE playlist_id = '".$playlist_id."'");
							$cached['playlist'] = $str;
						}
					}
				}
			}
			elseif ($add_id) {
				$z = explode(',',$playlist);
				if (!in_array($add_id,$z)) {
					$mysql->query("UPDATE ".$tb_prefix."playlist SET playlist_contents = CONCAT('".$add_id.",',playlist_contents) WHERE playlist_id = '".$playlist_id."'");
					$cached['playlist'] = $playlist.','.$add_id;
				}
				else $cached['playlist'] = $playlist;
			}
		}
	}
	$html = box_playlist(1);
	echo $html;
	exit();
}
if ($tv) {
    $id = $tv;
	$sql = "SELECT * FROM ".$tb_prefix."user 
                INNER JOIN ".$tb_prefix."playlist ON ".$tb_prefix."user.user_id = ".$tb_prefix."playlist.playlist_user_id
                WHERE playlist_id ='".$id."'";
	$q 								= $mysql->query($sql);
	$r 							= $mysql->fetch_array($q);
        $playlist_name = $r['playlist_name'];
	$user_id = $r['user_id'];
	$user_name 					= $r['user_name'];
	$user_sex 					= ($r['user_sex'] == 1)?"Nam":"Nữ";
	$user_email					= $r['user_email'];
	$user_fullname				= $r['user_fullname'];
	if($user_fullname == '')
	{
		$user_fullname = 'Vô Danh';
	}	
	$user_ym					= $r['user_ym'];
	if($user_ym == '')
	{
		$user_ym = 'Chưa có';
	}		
	$user_signature				= $r['user_signature'];
	if($user_signature == '')
	{
		$user_signature = 'MatXac.Com thế giới nhạc sàn sôi động';
	}		
	$avatar					= $r['user_avatar'];
	if($avatar == '')
	{
		$avatar = 'http://matxac.com/MATXAC2.jpg';
	}	
	$level = $r['user_level'];	
	if( $level == 3 )
	{
		$user_name = "<font color=#CC0000 >".$user_name."</font>";
	}
	if( $level == 2 )
	{
		$user_name = "<font color=#FF9900 >".$user_name."</font>";
	}		
		if (strstr($email,'@yahoo.com')) {
			$e 				= explode('@yahoo.com',$email);
			$user_email 			= "<a href=ymsgr:sendIM?".$e[0]."><img src='http://opi.yahoo.com/online?u=".$e[0]."'></a> ".$email;
		}	
	switch ($r['user_level']) {
		case 1	:	$user_level 	= "Member"; break;
		case 2	:	$user_level 	= "Moderator"; break;
		case 3	:	$user_level 	= "Admin"; break;
	}	
	
	$q = $mysql->query("SELECT playlist_id FROM ".$tb_prefix."playlist WHERE playlist_id = '".$id."'");
	if ($mysql->num_rows($q)) {
		$r = $mysql->fetch_array($q);
	
		$playlist = $id;
		$url_play = $mainURL.'/Playlist/4/'.$playlist.'.xml';
		include("templates/".$_SESSION['current_tpl']."/play_playlist.html");
	}
	else echo("<b><center>Playlist rỗng.</center></b>");
}
?>